<?php

namespace App\Jobs\FuYou;


use App\Models\FuYou\WorkOrderFuyouInfoModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Service\FuYou\FuYouService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class FuYouAutoJob implements ShouldQueue
{

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private array $data;
    private int   $type;

    const TYPE_DEFAULT         = 0;
    const TYPE_UPDATE_PAY_INFO = 1;

    const TYPE_MAPPING = [
        self::TYPE_DEFAULT         => '商户信息登记',
        self::TYPE_UPDATE_PAY_INFO => '修改收款信息',
    ];


    /**
     * 富友自动进件
     * @param array $data
     * @param int $type 默认0-商户信息登记 1-修改收款信息
     */
    public function __construct(array $data, int $type = 0)
    {
        $this->data = $data;
        $this->type = $type;
    }

    /**
     * 富友自动进件
     * @return void
     * @throws GuzzleException
     */
    public function handle()
    {
        $uuid = Str::uuid()->toString();
        Log::info("富友自动进件 Queue debug...{$uuid}", ['data' => $this->data, 'type' => $this->type]);

        $applyMain = WorkOrderApplyMainModel::getDetailByWorkOrderNo($this->data['workOrderNo']);
        $mode      = attributeGet($applyMain, 'openShopMainProcessRel.signOrderRel.contractRel.mode', 0);

        if ($mode == OpenShopContractTemplateModel::MODE_2) {// 分账模式工单自动带入「厦门心有炸念」账号信息-不走自动进件
            Log::info("富友自动进件 Queue 分账模式不走自动进件...{$uuid}", ['data' => $this->data, 'mode' => $mode]);
            $this->delete();
            return;
        }

        //判断是否是第一次提交-富友商户信息表
        $getInfo      = WorkOrderFuyouInfoModel::getInfoByWorkOrderNo($this->data['workOrderNo']);
        $fuYouService = new FuYouService();
        switch ($this->type) {
            case self::TYPE_DEFAULT:// 商户信息登记
                if ($getInfo) {
                    $fuYouService->wxMchntUpd(['workOrderNo' => $this->data['workOrderNo'], 'shopId' => $this->data['shopId']]);//商户信息更新接口
                } else {
                    $fuYouService->wxMchntAdd(['workOrderNo' => $this->data['workOrderNo'], 'shopId' => $this->data['shopId']]);//商户信息登记接口
                }
                break;
            case self::TYPE_UPDATE_PAY_INFO:// 修改收款信息接口
                if(!$getInfo){
                    Log::info("富友自动进件 Queue 未找到富友商户信息...{$uuid}", ['data' => $this->data]);
                    $this->delete();
                    return;
                }
//                $fuYouService->updatePayInfo(['workOrderNo' => $this->data['workOrderNo'], 'shopId' => $this->data['shopId']]);
                break;
        }
        Log::info("富友自动进件 Queue end...{$uuid}", ['data' => $this->data, 'type' => $this->type]);
    }
}
